import 'package:flutter/material.dart';

class MedicationEditPage extends StatefulWidget {
  const MedicationEditPage({Key? key}) : super(key: key);

  @override
  State<MedicationEditPage> createState() => _MedicationEditPageState();
}

class _MedicationEditPageState extends State<MedicationEditPage> {
  String _selectedMedication = 'med1';
  TimeOfDay _selectedTime = TimeOfDay(hour: 8, minute: 30);
  List<bool> _selectedDays = List.generate(7, (index) => false);
  String _selectedDosage = '250 mg';
  String _selectedFrequency = '一日一次';
  bool _beforeMeal = true;

  final List<String> _medications = [
    'med1',
    'med2',
    'med3',
    'med4',
    'med5',
    'med6'
  ];
  final List<String> _dosages = ['100 mg', '250 mg', '500 mg'];
  final List<String> _frequencies = ['一日一次', '一日两次', '一日三次'];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFF1DC4A0),
      appBar: AppBar(
        backgroundColor: Colors.transparent,
        elevation: 0,
        leading: IconButton(
          icon: Icon(Icons.arrow_back, color: Colors.white),
          onPressed: () => Navigator.pop(context),
        ),
        title: Text(
          '编辑用药',
          style: TextStyle(
            color: Colors.white,
            fontSize: 20,
            fontWeight: FontWeight.bold,
          ),
        ),
      ),
      body: Container(
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(30),
            topRight: Radius.circular(30),
          ),
        ),
        child: Column(
          children: [
            Expanded(
              child: SingleChildScrollView(
                padding: EdgeInsets.all(20),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      '药品名称',
                      style: TextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    SizedBox(height: 10),
                    Container(
                      height: 100,
                      child: ListView.builder(
                        scrollDirection: Axis.horizontal,
                        itemCount: _medications.length,
                        itemBuilder: (context, index) {
                          final medication = _medications[index];
                          return GestureDetector(
                            onTap: () => setState(
                                () => _selectedMedication = medication),
                            child: Container(
                              width: 80,
                              margin: EdgeInsets.only(right: 10),
                              decoration: BoxDecoration(
                                color: _selectedMedication == medication
                                    ? Color(0xFF1DC4A0).withOpacity(0.1)
                                    : Colors.grey.shade100,
                                borderRadius: BorderRadius.circular(15),
                                border: Border.all(
                                  color: _selectedMedication == medication
                                      ? Color(0xFF1DC4A0)
                                      : Colors.transparent,
                                ),
                              ),
                              child: Image.asset(
                                'assets/icons/$medication.png',
                                width: 60,
                                height: 60,
                              ),
                            ),
                          );
                        },
                      ),
                    ),
                    SizedBox(height: 20),
                    Text(
                      '服药时间',
                      style: TextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    SizedBox(height: 10),
                    GestureDetector(
                      onTap: () async {
                        final TimeOfDay? time = await showTimePicker(
                          context: context,
                          initialTime: _selectedTime,
                        );
                        if (time != null) {
                          setState(() => _selectedTime = time);
                        }
                      },
                      child: Container(
                        padding:
                            EdgeInsets.symmetric(horizontal: 15, vertical: 12),
                        decoration: BoxDecoration(
                          color: Colors.grey.shade100,
                          borderRadius: BorderRadius.circular(10),
                        ),
                        child: Row(
                          children: [
                            Icon(Icons.access_time, color: Colors.grey),
                            SizedBox(width: 10),
                            Text(
                              '${_selectedTime.format(context)}',
                              style: TextStyle(fontSize: 16),
                            ),
                          ],
                        ),
                      ),
                    ),
                    SizedBox(height: 20),
                    Text(
                      '重复',
                      style: TextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    SizedBox(height: 10),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: [
                        _buildDayButton('Mon', 0),
                        _buildDayButton('Tue', 1),
                        _buildDayButton('Wed', 2),
                        _buildDayButton('Thu', 3),
                        _buildDayButton('Fri', 4),
                        _buildDayButton('Sat', 5),
                        _buildDayButton('Sun', 6),
                      ],
                    ),
                    SizedBox(height: 20),
                    Row(
                      children: [
                        Expanded(
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(
                                '用量',
                                style: TextStyle(
                                  fontSize: 16,
                                  fontWeight: FontWeight.bold,
                                ),
                              ),
                              SizedBox(height: 10),
                              _buildDropdown(
                                value: _selectedDosage,
                                items: _dosages,
                                onChanged: (value) {
                                  if (value != null) {
                                    setState(() => _selectedDosage = value);
                                  }
                                },
                              ),
                            ],
                          ),
                        ),
                        SizedBox(width: 20),
                        Expanded(
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(
                                '频率',
                                style: TextStyle(
                                  fontSize: 16,
                                  fontWeight: FontWeight.bold,
                                ),
                              ),
                              SizedBox(height: 10),
                              _buildDropdown(
                                value: _selectedFrequency,
                                items: _frequencies,
                                onChanged: (value) {
                                  if (value != null) {
                                    setState(() => _selectedFrequency = value);
                                  }
                                },
                              ),
                            ],
                          ),
                        ),
                      ],
                    ),
                    SizedBox(height: 20),
                    Text(
                      '服用时间',
                      style: TextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    SizedBox(height: 10),
                    Row(
                      children: [
                        Expanded(
                          child: GestureDetector(
                            onTap: () => setState(() => _beforeMeal = true),
                            child: Container(
                              padding: EdgeInsets.symmetric(vertical: 12),
                              decoration: BoxDecoration(
                                color: _beforeMeal
                                    ? Color(0xFF1DC4A0).withOpacity(0.1)
                                    : Colors.grey.shade100,
                                borderRadius: BorderRadius.circular(10),
                                border: Border.all(
                                  color: _beforeMeal
                                      ? Color(0xFF1DC4A0)
                                      : Colors.transparent,
                                ),
                              ),
                              child: Center(
                                child: Text(
                                  '饭前',
                                  style: TextStyle(
                                    color: _beforeMeal
                                        ? Color(0xFF1DC4A0)
                                        : Colors.black,
                                    fontWeight: FontWeight.bold,
                                  ),
                                ),
                              ),
                            ),
                          ),
                        ),
                        SizedBox(width: 20),
                        Expanded(
                          child: GestureDetector(
                            onTap: () => setState(() => _beforeMeal = false),
                            child: Container(
                              padding: EdgeInsets.symmetric(vertical: 12),
                              decoration: BoxDecoration(
                                color: !_beforeMeal
                                    ? Color(0xFF1DC4A0).withOpacity(0.1)
                                    : Colors.grey.shade100,
                                borderRadius: BorderRadius.circular(10),
                                border: Border.all(
                                  color: !_beforeMeal
                                      ? Color(0xFF1DC4A0)
                                      : Colors.transparent,
                                ),
                              ),
                              child: Center(
                                child: Text(
                                  '饭后',
                                  style: TextStyle(
                                    color: !_beforeMeal
                                        ? Color(0xFF1DC4A0)
                                        : Colors.black,
                                    fontWeight: FontWeight.bold,
                                  ),
                                ),
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
                  ],
                ),
              ),
            ),
            Container(
              padding: EdgeInsets.all(20),
              child: ElevatedButton(
                onPressed: () {
                  // 创建新的用药提醒数据
                  final medicationData = {
                    'iconName': _selectedMedication,
                    'name': '药品${_selectedMedication.substring(3)}',
                    'dosage': _selectedDosage,
                    'frequency': '${_selectedFrequency}，1 片',
                    'time':
                        '${_selectedTime.format(context)} - ${_beforeMeal ? "饭前" : "饭后"}服用',
                    'days': _selectedDays,
                  };

                  // 返回数据到上一页
                  Navigator.pop(context, medicationData);
                },
                style: ElevatedButton.styleFrom(
                  backgroundColor: Color(0xFF1DC4A0),
                  padding: EdgeInsets.symmetric(vertical: 15),
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(10),
                  ),
                ),
                child: Text(
                  '保存',
                  style: TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildDayButton(String label, int index) {
    return GestureDetector(
      onTap: () => setState(() => _selectedDays[index] = !_selectedDays[index]),
      child: Container(
        width: 40,
        height: 40,
        decoration: BoxDecoration(
          color: _selectedDays[index]
              ? Color(0xFF1DC4A0).withOpacity(0.1)
              : Colors.grey.shade100,
          borderRadius: BorderRadius.circular(20),
          border: Border.all(
            color:
                _selectedDays[index] ? Color(0xFF1DC4A0) : Colors.transparent,
          ),
        ),
        child: Center(
          child: Text(
            label,
            style: TextStyle(
              color: _selectedDays[index] ? Color(0xFF1DC4A0) : Colors.black,
              fontSize: 12,
              fontWeight: FontWeight.bold,
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildDropdown({
    required String value,
    required List<String> items,
    required ValueChanged<String?> onChanged,
  }) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 15),
      decoration: BoxDecoration(
        color: Colors.grey.shade100,
        borderRadius: BorderRadius.circular(10),
      ),
      child: DropdownButton<String>(
        value: value,
        isExpanded: true,
        underline: SizedBox(),
        items: items
            .map((item) => DropdownMenuItem(
                  value: item,
                  child: Text(item),
                ))
            .toList(),
        onChanged: onChanged,
      ),
    );
  }
}
